package com.hopu.blb_erp.controller;

import com.hopu.blb_erp.entity.Functions;
import com.hopu.blb_erp.entity.PageEntity;
import com.hopu.blb_erp.service.FunctionsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api
@RestController
public class FunctionsController {

    @Autowired
    private FunctionsService functionsService;

    @ApiOperation("按用户名查询权限")
    @GetMapping("/functions/username")
    public ResponseEntity<List<Functions>> findFunctionsByName() {
        //从SpringSecurity取得登录的用户名
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<Functions> functions = functionsService.findByName(user.getUsername());
        return ResponseEntity.ok(functions);
    }

    @ApiOperation("按角色id查询权限")
    @GetMapping("/functions/{id}")
    public ResponseEntity<List<Functions>> findById(@PathVariable("id")String id) {
        List<Functions> functions = functionsService.findById(id);
        return ResponseEntity.ok(functions);
    }

    @ApiOperation("查询所有权限")
    @GetMapping("/functions")
    public ResponseEntity<List<Functions>> findAllFunctions(){
        List<Functions> functions = functionsService.findAll();
        return ResponseEntity.ok(functions);
    }

    @ApiOperation("分页查询权限")
    @GetMapping("/functions/{current}/{pageSize}")
    public ResponseEntity<PageEntity> findPage(@PathVariable("current")Integer current, @PathVariable("pageSize")Integer pageSize){
        // 查询角色总数
        Integer count = functionsService.findCount();
        // 分页查询订单
        List<Functions> functions = functionsService.findPage((current-1) * pageSize, pageSize);
        // 包装到分页对象
        PageEntity<Functions> page = new PageEntity<>();
        page.setCount(count);
        page.setData(functions);
        return ResponseEntity.ok(page);
    }

    @ApiOperation("添加权限")
    @PostMapping("/functions")
    public ResponseEntity<String> add(@RequestBody Functions functions){
        functionsService.add(functions);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("根据id更新权限")
    @PutMapping("/functions")
    public ResponseEntity<String> modify(@RequestBody Functions functions){
        functionsService.modifyById(functions);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("根据id删除权限")
    @DeleteMapping("/functions/{id}")
    public ResponseEntity<String> remove(@PathVariable("id")String id){
        functionsService.removeById(id);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("根据id查询权限")
    @GetMapping("/function/{id}")
    public ResponseEntity<Functions> findId(@PathVariable("id") String id){
        Functions functions = functionsService.findId(id);
        return ResponseEntity.ok(functions);
    }
}
